package com.game.server.sms;

import java.io.*;
import java.net.*;
import java.text.MessageFormat;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;

import lombok.extern.log4j.Log4j;


 public class SendSMS
 {
   static SendSMS instance;

   public static SendSMS getInstance()
  {
    if (instance == null)
     {
       instance = new SendSMS();
    }
    return instance;
  }



 public boolean sendSMS(String Mobile, String code)
  {

    try {

      //短信宝接口

      // https://api.smsbao.com/sms?u=yilingsan&p=01f538459cf44de09a6fc283bf9123ff&m={mobile}&c={content}

      String testUsername = "yilingsan"; //在短信宝注册的用户名
      String testPassword = "030420gy"; //在短信宝注册的密码
      String apikey = "01f538459cf44de09a6fc283bf9123ff"; //在短信宝注册的密码
      String testContent = "您的验证码是"+code+",５分钟内有效。若非本人操作请忽略此消息。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

      String httpUrl = "https://api.smsbao.com/sms?";

      StringBuffer httpArg = new StringBuffer(httpUrl);
      httpArg.append("u=").append(testUsername).append("&");
      httpArg.append("p=").append(apikey).append("&");
      httpArg.append("m=").append(Mobile).append("&");
      httpArg.append("c=").append(URLEncoder.encode(testContent));

      String requestUrl =  httpArg.toString();
      System.out.println(requestUrl);

      String requestMethod = "POST";
      URL url = new URL(requestUrl);

      LogManager.getInstance().Log("短信发送内容: testContent: "+ testContent +"url:"+requestUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setUseCaches(false);
      // 设置请求方式（GET/POST）
      conn.setRequestMethod(requestMethod);

      // 从输入流读取返回内容
      InputStream inputStream = conn.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(
          inputStream, "utf-8");
      BufferedReader bufferedReader = new BufferedReader(
          inputStreamReader);
      String str = null;
      StringBuffer buffer = new StringBuffer();
      while ((str = bufferedReader.readLine()) != null) {
        buffer.append(str);
      }




      // 释放资源
      bufferedReader.close();
      inputStreamReader.close();
      inputStream.close();
      inputStream = null;
      conn.disconnect();

      System.out.println(buffer.toString());
      System.out.println("==================================================");
      //jsonObject = JSONObject.fromObject(buffer.toString());

      //	log.info("sms result log: " + jsonObject.getString("stat") + " - " + jsonObject.getString("message") );

      if ("0".equals(buffer.toString())) {
          return true;
      }
    } catch (ConnectException ce) {
      LogManager.getInstance().Log("连接超时：{}"+ce);
    } catch (Exception e) {
      LogManager.getInstance().Log("连接超时：{}"+e);
    }
    return false;
  }
  
 @Test
  public void test()
   {
     try {

       //短信宝接口

       // https://api.smsbao.com/sms?u=yilingsan&p=01f538459cf44de09a6fc283bf9123ff&m={mobile}&c={content}
       String  content = "123123";
       String testUsername = "yilingsan"; //在短信宝注册的用户名
       String testPassword = "030420gy"; //在短信宝注册的密码
       String apikey = "01f538459cf44de09a6fc283bf9123ff"; //在短信宝注册的密码
       String testPhone = "18612700580" ;
       String testContent = "您的验证码是"+content+",５分钟内有效。若非本人操作请忽略此消息。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

       String httpUrl = "https://api.smsbao.com/sms?";

       StringBuffer httpArg = new StringBuffer(httpUrl);
       httpArg.append("u=").append(testUsername).append("&");
       httpArg.append("p=").append(apikey).append("&");
       httpArg.append("m=").append(testPhone).append("&");
       httpArg.append("c=").append(URLEncoder.encode(testContent));

       String requestUrl =  httpArg.toString();
       System.out.println(requestUrl);



       //	String requestUrl = "";
       String requestMethod = "POST";
       URL url = new URL(requestUrl);

       LogManager.getInstance().Log("短信发送内容: content: "+ content +"url:"+requestUrl);
       HttpURLConnection conn = (HttpURLConnection) url.openConnection();

       conn.setDoOutput(true);
       conn.setDoInput(true);
       conn.setUseCaches(false);
       // 设置请求方式（GET/POST）
       conn.setRequestMethod(requestMethod);

       // 从输入流读取返回内容
       InputStream inputStream = conn.getInputStream();
       InputStreamReader inputStreamReader = new InputStreamReader(
           inputStream, "utf-8");
       BufferedReader bufferedReader = new BufferedReader(
           inputStreamReader);
       String str = null;
       StringBuffer buffer = new StringBuffer();
       while ((str = bufferedReader.readLine()) != null) {
         buffer.append(str);
       }




       // 释放资源
       bufferedReader.close();
       inputStreamReader.close();
       inputStream.close();
       inputStream = null;
       conn.disconnect();

       System.out.println(buffer.toString());
       System.out.println("==================================================");
       //jsonObject = JSONObject.fromObject(buffer.toString());

       //	log.info("sms result log: " + jsonObject.getString("stat") + " - " + jsonObject.getString("message") );

       if ("0".equals(buffer.toString())) {
         //result = true;
       }

       //			if ("100".equals(jsonObject.getString("stat"))) {
       //				result = true;
       //			}
     } catch (ConnectException ce) {
       LogManager.getInstance().Log("连接超时：{}"+ce);
     } catch (Exception e) {
       LogManager.getInstance().Log("连接超时：{}"+e);
     }
    // return true;
  }
  


  }
  